﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CharacterTrailClient.Message;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Model;
using CharacterTrailLibrary.Persist.BLL.Model;
using CharacterTrailLibrary.Security;
using DevExpress.XtraEditors;

namespace CharacterTrailClient.Controls
{
    public partial class ComponentScanEdit : DevExpress.XtraEditors.XtraUserControl
    {
        public const string Component = "Component";
        public const string ComponentName = "元件";

        public delegate void InputBarCodeSuccessEndEventHandler(ProductHeaderContainBase productHeaderContainBase);

        private readonly IProductPersist _productPersist = null;
        public readonly TProductBaseModel ProductBaseModel = new TProductBaseModel();
 
        private readonly IStaticDataQuerier _staticDataQuerier;
        private TProductHeaderModel _productHeaderModel;
  

        public ComponentScanEdit()
        {
            InitializeComponent();
            _productPersist = InstanceFactory.GetProductPersist();
            _staticDataQuerier = InstanceFactory.GetStaticDataQuerier();
            this.textEditComponentBarCode.Enabled = false;
          
        }

        public event InputBarCodeSuccessEndEventHandler InputBarCodeSuccessEnd;

        private void CmdAddBaseInfo_Click(object sender, EventArgs e)
        {
            try
            {
                ProductBaseModel.OperateMember = memoEditOperateMember.Text;
                ProductBaseModel.ProduceEnvironment = memoEditProduceEnvironment.Text;
                ProductBaseModel.ProduceWay = memoEditProduceWay.Text;
                ProductBaseModel.GoodsState = memoEditGoodsState.Text;
                ProductBaseModel.MachineState = memoEditMachineState.Text;

                ProductBaseModel.RowGuid = Guid.NewGuid().ToString("N");
                ProductBaseModel.InputDate = DateTime.Now;
                ProductBaseModel.InputId = UserSession.CurrentUser.UserId;
                ProductBaseModel.InputName = UserSession.CurrentUser.UserName;
                _productPersist.InsertProductBaseModel(ProductBaseModel);
  
                this.labelControlMsg.Text = String.Format("提示信息：{0}", "新增成功！");

                this.CmdAddBaseInfo.Enabled = false;

                this.textEditComponentBarCode.Enabled = true;
                this.textEditComponentBarCode.Focus();
            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }

        private void CmdChange_Click(object sender, EventArgs e)
        {
            this.CmdAddBaseInfo.Enabled = true;
            this.textEditComponentBarCode.Enabled = false;
            this.memoEditOperateMember.Focus();
        }

        private void TextEditComponentBarCodeKeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == (char)Keys.Enter && !String.IsNullOrEmpty(this.textEditComponentBarCode.Text.Trim()))
                {

                    if (!String.IsNullOrEmpty(textEditComponentBarCode.Text.Trim()))
                    {
                        //查询条码表，必须在条码表中找到记录才行
                        var printlogModel = _staticDataQuerier.GetlmbcprintlogModel(textEditComponentBarCode.Text.Trim());
                        if (printlogModel == null)
                        {
                            ResetBarCode(string.Format("不存在该条码:{0}！", textEditComponentBarCode.Text.Trim()), Color.Red);
                        }
                        else
                        {
                            //查物料表，看是否物料在系统中
                            var iCItemModel = _staticDataQuerier.GetVIcItemQueryModel(printlogModel.Fitemid);

                            var productHeaderModelTemp =
                                _productPersist.GetProductHeaderModel(textEditComponentBarCode.Text.Trim(),Component);

                            if(productHeaderModelTemp != null)
                            {
                                ResetBarCode(string.Format("元件：{0}已经存在于系统中！", textEditComponentBarCode.Text.Trim()), Color.Red);
                                return;
                            }

                            _productHeaderModel = new TProductHeaderModel
                                                      {
                                                          ProductBaseRowGuid = ProductBaseModel.RowGuid,
                                                          ProcedureNo = Component,
                                                          ProcedureName = ComponentName,
                                                          OrderId =this.textEditOrderId.Text,
                                                          UseStatus= 1,
                                                          TranType=3,
                                                          InputId=UserSession.CurrentUser.UserName,
	                                                      InputDate=DateTime.Now,
                                                          ProductItemBarCode = textEditComponentBarCode.Text.Trim(),
                                                          ProductItemId = printlogModel.Fitemid,
                                                          ProductItemNumber = iCItemModel.FNumber,
                                                          ProductItemName = iCItemModel.FName
                                                      };
                            try
                            {
                                _productPersist.SaveProductHeaderModels(new List<TProductHeaderModel>() { _productHeaderModel });
                            }
                            catch (Exception ex)
                            {
                                ResetBarCode(string.Format("错误：{0}！", ex.Message), Color.Red);
                                return;
                            }

                            if (InputBarCodeSuccessEnd != null)
                            {
                                var model = new ProductHeaderContainBase(_productHeaderModel, ProductBaseModel);
                                InputBarCodeSuccessEnd(model);
                            }
                            ResetBarCode(string.Format("条码{0}成功记录！", ""), Color.Blue);
                        }
                    }
                    else
                    {
                        _productHeaderModel = null;
                    }
                }
            }
            catch (Exception exception)
            {
                MsgBox.ShowError(exception.Message);
            }
        }

        private void ResetBarCode(string msg,Color color)
        {
            this.textEditComponentBarCode.Text = "";
            this.textEditMessage.Text = msg;
            this.textEditMessage.ForeColor = color;
            this.textEditComponentBarCode.Focus();
        }
    }
}
